package DataBaseTest;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import java.awt.Color;
import java.awt.Font;

public class DataBaseTest_0528 extends JFrame {
	Insert insert;
	Update update;
	Delete delete;
	Select select;
	
	
	String driver="oracle.jdbc.driver.OracleDriver";
	String url="jdbc:oracle:thin:@127.0.0.1:1521:xe";
	String db_user="hr";
	String db_pwd="hr";
	
	Connection conn;
	
	
	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	private JMenuItem mntmNewMenuItem_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DataBaseTest_0528 frame = new DataBaseTest_0528();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DataBaseTest_0528() {
		setTitle("데이터베이스 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 792, 753);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
	//데이터 베이스 연결 테스트
	//1)드라이버 로딩
		try{
		Class.forName(driver).newInstance();
		System.out.println("드라이버가 로딩되었습니다.");
		
	//2)Connection 객체 생성
		conn=DriverManager.getConnection(url,db_user,db_pwd);
		System.out.println("데이터 베이스가 정상적으로 연결됨");
		}catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
		}
		return menuBar;
	}

	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("New menu");
			mnNewMenu.setFont(new Font("서울남산 장체L", Font.PLAIN, 13));
			mnNewMenu.add(getMntmNewMenuItem());
			mnNewMenu.add(getMntmNewMenuItem_2());
			mnNewMenu.add(getMntmNewMenuItem_1());
			mnNewMenu.add(getMntmNewMenuItem_3());
		}
		return mnNewMenu;
	}
public void openInsert(){	
	if (insert == null) {
	insert = new Insert(conn,this);
	contentPane.add(insert);
}
insert.setVisible(true);
insert.toFront();
}

public void openUpdate(){	
	if (update == null) {
	update = new Update(conn,this);
	contentPane.add(update);
}
update.setVisible(true);
update.toFront();
}
public void openSelect(){
	if (select == null) {
		select = new Select(conn,this);
		contentPane.add(select);
	}
	select.setVisible(true);
}
public void openDelete(){
	if (delete == null) {
		delete = new Delete(conn,this);
		contentPane.add(delete);
	}
	delete.setVisible(true);
}

	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("입력");
			mntmNewMenuItem.setFont(new Font("서울남산 장체L", Font.PLAIN, 13));
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				openInsert();
				}
			});
		}
		return mntmNewMenuItem;
	}

	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("삭제");
			mntmNewMenuItem_1.setFont(new Font("서울남산 장체L", Font.PLAIN, 13));
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openDelete();
				}
			});
		}
		return mntmNewMenuItem_1;
	}

	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("수정");
			mntmNewMenuItem_2.setFont(new Font("서울남산 장체L", Font.PLAIN, 13));
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openUpdate();
				}
			});
		}
		return mntmNewMenuItem_2;
	}

	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("조회");
			mntmNewMenuItem_3.setFont(new Font("서울남산 장체L", Font.PLAIN, 13));
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openSelect();
				}
			});
		}
		return mntmNewMenuItem_3;
	}
}
